<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的购物车 - 网上商城</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body {
            background-color: #f8f9fa; /* 浅灰色背景 */
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
        }
        .navbar {
            margin-bottom: 20px;
        }
        .user-dashboard {
            display: flex;
            min-height: calc(100vh - 150px); /* 减去导航栏和底部的高度 */
        }
        .sidebar {
            flex: 0 0 220px; /* 固定侧边栏宽度 */
            background-color: #343a40; /* 深色背景 */
            padding-top: 20px;
            color: white;
            border-radius: 8px; /* 圆角 */
            margin-right: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.75);
            padding: 12px 15px;
            transition: background-color 0.2s, color 0.2s;
            display: flex;
            align-items: center;
        }
        .sidebar .nav-link:hover {
            background-color: #495057; /* 悬停背景色 */
            color: white;
        }
        .sidebar .nav-link.active {
            background-color: #007bff; /* 选中项背景色 */
            color: white;
        }
        .sidebar .nav-link i {
            margin-right: 10px;
        }
        .main-content {
            flex-grow: 1; /* 主内容区占据剩余空间 */
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.05);
        }
        h2 {
            color: #333;
            margin-bottom: 25px;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        .cart-items-table .item-img {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 4px;
        }
        .table thead th {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }
        .table tbody tr:hover {
            background-color: #f2f2f2;
        }
        .table td, .table th {
            vertical-align: middle; /* 垂直居中 */
        }
        .total-section {
            background-color: #e9ecef;
            padding: 20px;
            border-radius: 8px;
            margin-top: 30px;
            border: 1px solid #dee2e6;
            text-align: right;
            font-size: 1.2em;
        }
        .total-section .total-price {
            font-size: 1.8em;
            font-weight: bold;
            color: #dc3545; /* 红色，突出总价 */
        }
        .cart-actions {
            margin-top: 30px;
            text-align: center;
        }
        .cart-actions .btn {
            margin: 0 10px;
        }
        .no-items-message {
            text-align: center;
            padding: 50px;
            color: #6c757d;
            font-size: 1.1em;
            background-color: #e9ecef;
            border-radius: 8px;
            margin-top: 30px;
        }
        .footer {
            background-color: #343a40;
            color: white;
            padding: 20px 0;
            text-align: center;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="{:Url('index/index/index')}">网上商城</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    {if condition="$Request.session.user_id"}
                        <li class="nav-item">
                            <a class="nav-link" href="#">欢迎, {$Request.session.username}</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/cart')}">购物车</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/myOrders')}">我的订单</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/logout')}">退出登录</a>
                        </li>
                    {else /}
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/login')}">登录</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/register')}">注册</a>
                        </li>
                    {/if}
                    <li class="nav-item">
                        <a class="nav-link" href="{:Url('admin/index/adminLogin')}">后台管理</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container user-dashboard">
        <div class="sidebar">
            <h4 class="text-center mb-4 pt-3">用户中心</h4>
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link active" href="{:Url('index/index/cart')}">
                        <i class="bi bi-cart-fill"></i> 我的购物车
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{:Url('index/index/myOrders')}">
                        <i class="bi bi-box-seam-fill"></i> 我的订单
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{:Url('index/index/logout')}">
                        <i class="bi bi-box-arrow-right"></i> 退出登录
                    </a>
                </li>
            </ul>
        </div>

        <div class="main-content">
            <h2>我的购物车</h2>

            {if empty($cartItems)}
                <div class="no-items-message">
                    <i class="bi bi-info-circle-fill me-2"></i> 您的购物车还是空的！
                    <p class="mt-3"><a href="{:Url('index/index/index')}" class="btn btn-primary">前往购物</a></p>
                </div>
            {else /}
                <div class="table-responsive">
                    <table class="table table-striped table-hover align-middle cart-items-table">
                        <thead>
                            <tr>
                                <th>商品图片</th>
                                <th>商品名称</th>
                                <th>单价</th>
                                <th>数量</th>
                                <th>小计</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {foreach name="cartItems" item="item"}
                            <tr>
                                <td>
                                    {if isset($item.product) && !empty($item.product.pImg)}
                                        <a href="{:Url('index/index/productDetail', ['id' => $item.product.pId])}">
                                            <img src="/tp/public/static/upload/{$item.product.pImg}" alt="{$item.product.pName}" class="item-img">
                                        </a>
                                    {else /}
                                        <img src="/tp/public/static/img/no_image.png" alt="无图片" class="item-img">
                                    {/if}
                                </td>
                                <td>
                                    <a href="{:Url('index/index/productDetail', ['id' => $item.product_id])}">
                                        {$item.product.pName}
                                    </a>
                                </td>
                                <td>¥ {$item.product.pPrice}</td>
                                <td>
                                    <form action="{:Url('index/index/updateCart')}" method="post" class="d-flex align-items-center">
                                        <input type="hidden" name="cart_id" value="{$item.id}">
                                        <input type="number" name="quantity" value="{$item.quantity}" min="1" max="{$item.product.stock}" class="form-control form-control-sm me-2" style="width: 80px;">
                                        <button type="submit" class="btn btn-outline-primary btn-sm" title="更新数量">
                                            <i class="bi bi-arrow-counterclockwise"></i>
                                        </button>
                                    </form>
                                </td>
                                <td>¥ {$item.quantity * $item.product.pPrice}</td>
                                <td>
                                    <form action="{:Url('index/index/removeCartItem')}" method="post" onsubmit="return confirm('确定要从购物车中移除此商品吗？');">
                                        <input type="hidden" name="cart_id" value="{$item.id}">
                                        <button type="submit" class="btn btn-danger btn-sm" title="移除商品">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            {/foreach}
                        </tbody>
                    </table>
                </div>

                <div class="total-section">
                    <p>总商品数: {$cartItems|count}</p>
                    <p class="total-price">总计: ¥ {$totalPrice}</p>
                </div>

                <div class="cart-actions">
                    <a href="{:Url('index/index/index')}" class="btn btn-secondary btn-lg">
                        <i class="bi bi-arrow-left-circle me-2"></i> 继续购物
                    </a>
                    <a href="{:Url('index/index/checkout')}" class="btn btn-success btn-lg">
                        <i class="bi bi-cart-check-fill me-2"></i> 前往结算
                    </a>
                </div>
            {/if}
        </div>
    </div>

    <footer class="footer">
        <div class="container">
            <p>&copy; 2024 网上商城. 保留所有权利。</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>